package co.edu.udistrital.analyzer.web.managedbean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import co.edu.udistrital.analyzer.business.orm.model.Param;
import co.edu.udistrital.analyzer.business.orm.model.enums.ParamName;
import co.edu.udistrital.analyzer.business.services.ParamsService;

/**
 * Managed bean for the params process
 * 
 * @author Alejo
 * 
 */
@Component
@Scope("session")
public class ParamsBean extends AbstractCommonBean {

	/**
	 * Serial version id
	 */
	private static final long serialVersionUID = 3192730012610978808L;
	/**
	 * Dao for the user database operations
	 */
	@Autowired
	private ParamsService paramsService;
	/**
	 * The application params
	 */
	private List<Param> params;
	
	public ParamsBean () {
		params = new ArrayList<Param>();
	}

	public String save() {
		for (Param param : params) {
			paramsService.saveOrUpdateParam(param);
		}
		showMessage(FacesMessage.SEVERITY_INFO, "Info",
				"Los parametros han sido modificados correctamente");
		return "";
	}

	/**
	 * @return Retorna el valor de params
	 */
	public List<Param> getParams() {
		if ((params == null || params.isEmpty())
				&& paramsService != null) {
			for (ParamName paramName: ParamName.values()) {
				Param param = paramsService.findParamByKey(paramName);
				if (param == null) {
					param = new Param();
					param.setKey(paramName);
				}
				params.add(param);
			}
		}
		return params;
	}

	/**
	 * Asigna un nuevo valor aparams
	 * 
	 * @param params Nuevo valor de params
	 */
	public void setParams(List<Param> params) {
		this.params = params;
	}
}